/******************************************************************************
 Copyright (c) 2019-2024 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/
#ifndef __CSR_BT_TD_DB_SC_H_
#define __CSR_BT_TD_DB_SC_H_


#ifdef __cplusplus
extern "C" {
#endif

#include "csr_synergy.h"
#include "csr_types.h"
#include "csr_bt_addr.h"
#include "csr_bt_profiles.h"
#include "dm_prim.h"
#include "csr_bt_td_db.h"


/* Logical TDDB key used to store information to PS in CsrBtTdDbBredrKey format.*/
#define CSR_BT_TD_DB_SC_KEY_BREDR_KEY           0
/* Logical TDDB key used to store information to PS in CsrBtTdDbLeKeys format.*/
#define CSR_BT_TD_DB_SC_KEY_LE_KEYS             1

/* LE key type mask indicating no keys are present in the CsrBtTdDbLeKeys.*/
#define CSR_BT_TD_DB_LE_KEY_NONE                (0x00)
/* LE key type mask indicating encryption central key present in the CsrBtTdDbLeKeys.*/
#define CSR_BT_TD_DB_LE_KEY_ENC_CENTRAL         (0x01)
/* LE key type mask indicating identity key present in the CsrBtTdDbLeKeys.*/
#define CSR_BT_TD_DB_LE_KEY_ID                  (0x02)
/* LE key type mask indicating sign key present in the CsrBtTdDbLeKeys.*/
#define CSR_BT_TD_DB_LE_KEY_SIGN                (0x04)
/* LE key type mask indicating diversifier present in the CsrBtTdDbLeKeys.*/
#define CSR_BT_TD_DB_LE_KEY_DIV                 (0x08)

/* This stores all the required BREDR key information. This is represented by TDDB key CSR_BT_TD_DB_SC_KEY_BREDR_KEY.*/
typedef struct
{
    CsrUint8                linkkeyType;    /* Type of the link key. Refer to DM_SM_LINK_KEY_X in dm_prim.h for more information.*/
    CsrUint8                linkkeyLen;     /* Length of the link key in octets.*/
    CsrBtDeviceLinkkey      linkkey;        /* Link key value. Refer to CsrBtDeviceLinkkey for more information.*/
    CsrBool                 authorised;     /* Whether the key is authorised or not. */
    CsrUint8                pad;            /* Reserved for future use.*/
} CsrBtTdDbBredrKey;

typedef DM_SM_KEY_ENC_CENTRAL_T CsrBtScLeKeyEncCentral;
typedef DM_SM_KEY_ID_T CsrBtScLeKeyId;
typedef DM_SM_KEY_SIGN_T CsrBtScLeKeySign;

/* This stores all the required LE key information. This is represented by TDDB key CSR_BT_TD_DB_SC_KEY_LE_KEYS.*/
typedef struct
{
    CsrUint16               keyValid:4;         /* Key valid mask. please refer to CSR_BT_TD_DB_LE_KEY_X in
                                                 * for more information. */
    CsrUint16               rpaOnlyPresent:1;   /* This is set when the device is using RPA only addresses.*/
    CsrUint16               keySize:11;         /* Number of significant octets in LTK. */
    CsrUint16               secReq;             /* Security requirements bitfield. Refer to DM_SM_SECURITY_X in dm_prim.h
                                                 * for more information.*/
    CsrBtScLeKeyEncCentral  encCentral;         /* LTK key. Refer to DM_SM_KEY_ENC_CENTRAL_T in dm_prim.h */
    CsrUint16               div;                /* Diversifier used for key generation. */
    CsrBtScLeKeyId          id;                 /* ID key. Please refer to DM_SM_KEY_ID_T in dm_prim.h */
    CsrBtScLeKeySign        sign;               /* Sign key. Please refer to DM_SM_KEY_SIGN_T in dm_prim.h */
} CsrBtTdDbLeKeys;

CSR_BT_TD_DB_KEY_STRUCT_COMPILE_ASSERT(CsrBtTdDbBredrKey);
CSR_BT_TD_DB_KEY_STRUCT_COMPILE_ASSERT(CsrBtTdDbLeKeys);

#define CSR_BT_TD_DB_BREDR_KEY_VALID(_bredrKey) (_bredrKey->linkkeyType == DM_SM_LINK_KEY_NONE)

#define CsrBtTdDbSetBredrKey(_addressType, _deviceAddr, _bredrKey)          \
    CsrBtTdDbWriteEntry(_addressType,                                       \
                        _deviceAddr,                                        \
                        CSR_BT_TD_DB_SOURCE_SC,                             \
                        CSR_BT_TD_DB_SC_KEY_BREDR_KEY,                      \
                        sizeof(CsrBtTdDbBredrKey),                          \
                        _bredrKey)

#define CsrBtTdDbGetBredrKey(_addressType, _deviceAddr, _bredrKey)          \
    CsrBtTdDbGetEntry(_addressType,                                         \
                      _deviceAddr,                                          \
                      CSR_BT_TD_DB_SOURCE_SC,                               \
                      CSR_BT_TD_DB_SC_KEY_BREDR_KEY,                        \
                      sizeof(CsrBtTdDbBredrKey),                            \
                      _bredrKey)

#define CsrBtTdDbSetLeKeys(_addressType, _deviceAddr, _leKeys)              \
    CsrBtTdDbWriteEntry(_addressType,                                       \
                        _deviceAddr,                                        \
                        CSR_BT_TD_DB_SOURCE_SC,                             \
                        CSR_BT_TD_DB_SC_KEY_LE_KEYS,                        \
                        sizeof(CsrBtTdDbLeKeys),                            \
                        _leKeys)

#define CsrBtTdDbGetLeKeys(_addressType, _deviceAddr, _leKeys)              \
    CsrBtTdDbGetEntry(_addressType,                                         \
                      _deviceAddr,                                          \
                      CSR_BT_TD_DB_SOURCE_SC,                               \
                      CSR_BT_TD_DB_SC_KEY_LE_KEYS,                          \
                      sizeof(CsrBtTdDbLeKeys),                              \
                      _leKeys)

#ifdef __cplusplus
}
#endif

#endif /* __CSR_BT_TD_DB_SC_H_ */
